package com.fingard.dsp.bank.dbobj.receipt;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

/**单子回单映射*/
public class ReceiptMapping {

	/**标识出主键字段，三选一askNumber、receiptNo、busRef*/
	public String keyName="";
	
	public String getKeyValue() throws Exception{
		if(StringHelper.isNullOrEmpty(keyName)){
			throw new Exception("keyName不能为空");
		}
		String key1 = keyName.toUpperCase();
		if("busRef".toUpperCase().equals(key1)){
			return busRef;
		}else if("receiptNo".toUpperCase().equals(key1)){
			return receiptNo;
		}else if("askNumber".toUpperCase().equals(key1)){
			return askNumber;
		}else{
			throw new Exception("未知的keyName："+keyName);
		}
	}
	
	/**其它主键值*/
	private HashMap<String,String> otherBusRefList;
	public void addOtherBusRef(String pBusRef, String pReceiptNo){
		if(StringHelper.hasAnyChar(pBusRef) && pBusRef.trim().length()>0){
			if(otherBusRefList == null){
				otherBusRefList = new HashMap<String,String>();
			}
			if(!otherBusRefList.containsKey(pBusRef)){
				otherBusRefList.put(pBusRef, pReceiptNo);
			}
		}
	}
	public HashMap<String,String> getOtherBusRef(){
		return otherBusRefList;
	}
	
	public long urid;
	
	/**支付流水号*/
	public String askNumber="";
	
	/**支付批次号*/
	public String reqBatchNo="";
	
	/**回单编号*/
	public String receiptNo="";
	
	/**业务参考号*/
	public String busRef="";
	

	/**本方账号*/
	public String ownAct="";
	/**所属银行代码，如102工行，308招行*/
	public String bank="";
	public String moneyWay="";
	public BigDecimal amount;
	
	/**回单日期*/
	public int busDate = 0;
	
	public void setBusDate(Date pDate){
		busDate = Integer.parseInt(Format.Date8Format.format(pDate));
	}
	
	public Date getBusDate() throws ParseException{
		return Format.Date8Format.parse(busDate+"");
	}
	
	/**保存在文件服务器的文件Id，用于个性化项目文件服务接口*/
	public String customFileId="";
	
	/**回单文件相对路径*/
	public String receiptFile="";
	
	/**回单原始文件名*/
	public String srcFile="";
	
	public Date createdOn;

	/**对方户名*/
	public String oppActName="";

	/**对方账户*/
	public String oppActNum="";

	/** 币种 */
	public String cur;
	
	public void fromMap(Map<String,Object> pDbMp) throws ParseException{
		urid = Long.parseLong(String.valueOf(pDbMp.get("URID")));
		askNumber = String.valueOf(pDbMp.get("ASKNUMBER"));
		receiptNo = String.valueOf(pDbMp.get("RECEIPTNO"));
		busRef = String.valueOf(pDbMp.get("BUSREF"));
		reqBatchNo = String.valueOf(pDbMp.get("REQBATCHNO"));
		ownAct = String.valueOf(pDbMp.get("OWNACT"));
		bank = String.valueOf(pDbMp.get("BANK"));
		moneyWay = String.valueOf(pDbMp.get("MONEYWAY"));
		if(pDbMp.get("AMOUNT")!=null && String.valueOf(pDbMp.get("AMOUNT")).length() > 0){
			amount = new BigDecimal(String.valueOf(pDbMp.get("AMOUNT")));
		}
		if(pDbMp.get("BUSDATE") != null && String.valueOf(pDbMp.get("BUSDATE")).length() > 0){
			busDate = Integer.parseInt(String.valueOf(pDbMp.get("BUSDATE")));
		}
		customFileId = String.valueOf(pDbMp.get("CUSTOMFILEID"));
		receiptFile = String.valueOf(pDbMp.get("RECEIPTFILE"));
		srcFile = String.valueOf(pDbMp.get("SRCFILE"));
		String tt=String.valueOf(pDbMp.get("CREATEDON"));
		if(StringHelper.hasAnyChar(tt)){
			createdOn = new SimpleDateFormat(Format.DateIsoFormatStr).parse(tt);
		}
	}
	
	public String toXml(){
		//"<item fileName=\""+p_ref+"_"+p_act+fileExtAndDot+"\" customFileId=\""+customFileId+"\" ref=\""+p_ref+"\" srcFileName=\""+receiptFileName+"\" />\r\n"
		StringBuilder sb = new StringBuilder();
		sb.append("<item");
		sb.append(" urid=\""+urid+"\"");
		sb.append(" keyName=\""+keyName+"\"");
		sb.append(" askNumber=\""+askNumber+"\"");
		sb.append(" receiptNo=\""+receiptNo+"\"");
		sb.append(" busRef=\""+busRef+"\"");
		sb.append(" reqBatchNo=\""+reqBatchNo+"\"");
		sb.append(" ownAct=\""+ownAct+"\"");
		sb.append(" bank=\""+bank+"\"");
		sb.append(" moneyWay=\""+moneyWay+"\"");
		sb.append(" amount=\""+amount+"\"");
		sb.append(" busDate=\""+busDate+"\"");
		sb.append(" customFileId=\""+customFileId+"\"");
		sb.append(" receiptFile=\""+receiptFile+"\"");
		sb.append(" srcFile=\""+srcFile+"\"");
		if(createdOn!=null){
			sb.append(" createdOn=\""+new SimpleDateFormat(Format.DateIsoFormatStr).format(createdOn)+"\"");
		}else{
			sb.append(" createdOn=\"\"");
		}
		
		sb.append(" />\r\n");
		return sb.toString();
	}
	
	public void fromXml(XElement xe) throws Exception{
		if(xe!=null){
			if(StringHelper.hasAnyChar(xe.getAttrValue("urid"))){
				urid = Long.parseLong(xe.getAttrValue("urid"));
			}
			keyName = xe.getAttrValue("keyName");
			askNumber = xe.getAttrValue("askNumber");
			receiptNo = xe.getAttrValue("receiptNo");
			busRef = xe.getAttrValue("busRef");
			reqBatchNo = xe.getAttrValue("reqBatchNo");
			ownAct = xe.getAttrValue("ownAct");
			bank = xe.getAttrValue("bank");
			moneyWay = xe.getAttrValue("moneyWay");
			amount = new BigDecimal(xe.getAttrValue("amount"));
			busDate = Integer.parseInt(xe.getAttrValue("busDate"));
			customFileId = xe.getAttrValue("customFileId");
			receiptFile = xe.getAttrValue("receiptFile");
			srcFile = xe.getAttrValue("srcFile");
			String tt=xe.getAttrValue("createdOn");
			if(StringHelper.hasAnyChar(tt)){
				createdOn = new SimpleDateFormat(Format.DateIsoFormatStr).parse(tt);
			}
		}
	}
}
